package com.spyx.payment.service.impl;

import com.atyx.spyx.enums.PaymentStatus;
import com.atyx.spyx.enums.PaymentType;
import com.atyx.spyx.model.order.OrderInfo;
import com.atyx.spyx.model.order.PaymentInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spyx.common.constant.MqConst;
import com.spyx.common.service.RabbitService;
import com.spyx.exception.SpyxException;
import com.spyx.order.OrderClient;
import com.spyx.payment.mapper.PaymentInfoMapper;
import com.spyx.payment.service.PaymentInfoService;
import com.spyx.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo>
implements PaymentInfoService {
    @Override
    public PaymentInfo getPaymentInfoByOrderNo(String orderNo, PaymentType weixin) {
        //1.构建查询条件
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo,orderNo);
        wrapper.eq(PaymentInfo::getPaymentType,weixin);
        //2.执行查询
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }

    @Autowired
    private OrderClient orderClient;
    @Override
    public PaymentInfo savePaymentInfo(String orderNo, PaymentType weixin) {
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentType(weixin);
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);//支付中
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setSubject("张三买了三个西瓜");//交易内容
        //订单id
        //根据orderNo,查询orderId-远程调用service-order模块
        if(orderNo==null){
            throw  new SpyxException(ResultCodeEnum.DATA_ERROR);
        }
        OrderInfo orderInfo = orderClient.getOrderInfoByOrderNo(orderNo);
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
      //  paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setTotalAmount(new BigDecimal("0.01"));

        baseMapper.insert(paymentInfo);

        return paymentInfo;
    }

    @Autowired
    private RabbitService rabbitService;
    @Override
    public void paySuccess(String orderNo, PaymentType weixin, Map resultMap) {
        //1.更新订单的支付状态-远程调用serivce-order (数据库:shequ-order)
        orderClient.updateOrderStatus(orderNo);
        //2.更新payment_info表中支付状态(数据库:shequ-order)
        //2.1 先查询该支付数据的情况,判断是否是"已支付"
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo,orderNo);
        wrapper.eq(PaymentInfo::getPaymentType,weixin);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        //状态不等于 "未支付"
        /*   if(paymentInfo.getPaymentStatus()!=PaymentStatus.UNPAID){
            return;
        }*/
        if(paymentInfo.getPaymentStatus()==PaymentStatus.PAID){
            return;
        }
        //2.2 未支付
        paymentInfo.setPaymentStatus(PaymentStatus.PAID);//已支付
        String tradeNo =
                (String)(weixin == PaymentType.WEIXIN ? resultMap.get("transaction_id") : resultMap.get("trade_no"));//设置交易编号，注意:微信和支付宝的交易编号获取的方式不同
        paymentInfo.setTradeNo(tradeNo);
        paymentInfo.setCallbackTime(new Date()); //设置回调时间
        paymentInfo.setCallbackContent(resultMap.toString());//设置回调内容
        baseMapper.updateById(paymentInfo);
        //3.扣减库存-修改数据库(使用mq消息队列通知)-操作的是service-product模块
        rabbitService.sendMessage(MqConst.EXCHANGE_ORDER_DIRECT,MqConst.ROUTING_MINUS_STOCK,orderNo);

    }
}
